/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * bzr_giveaway.java
 *
 * Created on 25.04.2011, 17:41:03
 */
package bzr_lagerverwaltung;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import java.awt.Color;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Flo
 */
public class bzr_giveaway extends javax.swing.JFrame {

    public Connection con;
    DefaultComboBoxModel dcbm;

    /** Creates new form bzr_giveaway */
    public bzr_giveaway() throws InstantiationException, IllegalAccessException, SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String url = "jdbc:mysql://mysql.bezirksrundschau.com:3306/brdbm_1";
            con = (Connection) DriverManager.getConnection(url, "brdbm", "br2011!db");
            dcbm = new DefaultComboBoxModel();
            initComboBox();
            initComponents();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(bzr_giveaway.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldName = new javax.swing.JTextField();
        jFormattedTextFieldIst = new javax.swing.JFormattedTextField();
        jFormattedTextFieldMin = new javax.swing.JFormattedTextField();
        jButtonOK = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jComboBox_Lieferant = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Name:");

        jLabel2.setText("Stückzahl:");

        jLabel3.setText("Mindestbestand:");

        jFormattedTextFieldIst.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        jFormattedTextFieldIst.setText("0");

        jFormattedTextFieldMin.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        jFormattedTextFieldMin.setText("0");

        jButtonOK.setText("OK");
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        jLabel4.setText("Lieferant:");

        jComboBox_Lieferant.setModel(dcbm);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jTextFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jFormattedTextFieldMin, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jFormattedTextFieldIst, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
                                .addComponent(jButtonOK, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox_Lieferant, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(31, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jFormattedTextFieldIst, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jFormattedTextFieldMin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonOK))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jComboBox_Lieferant, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(60, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
        // TODO add your handling code here:
        if (testTextfields()) {
            try {
                Statement st = (Statement) con.createStatement();
                Lieferant l = (Lieferant) jComboBox_Lieferant.getSelectedItem();
                st.executeUpdate("INSERT INTO GiveAways (Name,Ist_Stueckzahl,Mindest_Stueckzahl,Lieferanten_L_ID)" + "VALUES ('" + jTextFieldName.getText() + "','" + jFormattedTextFieldIst.getText() + "'," + jFormattedTextFieldMin.getText() + ",'" + l.getId() + "')");

            } catch (SQLException ex) {
                Logger.getLogger(bzr_giveaway.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (JOptionPane.showConfirmDialog(this, "Wollen sie noch ein GiveAway hinzufügen") != JOptionPane.OK_OPTION) {
                this.setVisible(false);
            } else {
               jTextFieldName.setText("");
               jFormattedTextFieldIst.setText("0");
               jFormattedTextFieldMin.setText("0");
            }
        }
    }//GEN-LAST:event_jButtonOKActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new bzr_giveaway().setVisible(true);
                } catch (InstantiationException ex) {
                    Logger.getLogger(bzr_giveaway.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(bzr_giveaway.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(bzr_giveaway.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonOK;
    private javax.swing.JComboBox jComboBox_Lieferant;
    private javax.swing.JFormattedTextField jFormattedTextFieldIst;
    private javax.swing.JFormattedTextField jFormattedTextFieldMin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextFieldName;
    // End of variables declaration//GEN-END:variables

    private boolean testTextfields() {
        jTextFieldName.setBackground(Color.white);
        if (jTextFieldName.getText().equals("")) {
            jTextFieldName.setBackground(Color.yellow);
            JOptionPane.showMessageDialog(this, "Das Textfeld Namen ist leer!!!");
            return false;
        } else {
            return true;
        }
    }

    private void initComboBox() {
        try {
            Statement st = (Statement) con.createStatement();
            ResultSet rs = (ResultSet) st.executeQuery("SELECT * FROM Lieferanten");
            while (rs.next()) {
                Lieferant l = new Lieferant();
                l.setId(rs.getInt("Lieferant_ID"));
                l.setName(rs.getString("Name"));
                l.setOrt(rs.getString("Ort"));
                l.setPlz(rs.getInt("PLZ"));
                l.setStraße(rs.getString("Strasse"));
                l.setHnr(rs.getInt("HausNr"));
                dcbm.addElement(l);
            }
        } catch (SQLException ex) {
            Logger.getLogger(bzr_giveaway.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
